//
//  NoticeViewController.m
//  Eath
//
//  Created by yuhaya on 10/3/14.
//  Copyright (c) 2014 yuhaya. All rights reserved.
//

#import "NoticeViewController.h"
#import "StudentList.h"
#import "ContentViewController.h"
#import "Tool.h"
#import "RootViewController.h"
//#import "HistoryNavViewController.h"
#import "StudentListTableViewController.h"

@interface NoticeViewController () <DKScrollingTabControllerDelegate,UINavigationControllerDelegate>

@end
static Boolean waitNavigation = NO;

@implementation NoticeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:@"信息通告"];
    customLab.textAlignment = NSTextAlignmentCenter;
    customLab.font = [UIFont boldSystemFontOfSize:16];
    self.navigationItem.titleView = customLab;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"历史" style:UIBarButtonItemStylePlain target:self action:@selector(history)];
    
    self.stu_list_obj = [StudentList shareStudentList];
    
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateStulist:) name:STUS_CHANGE object:nil];
}

- (void)history {
//   HistoryNavViewController * history = [[HistoryNavViewController alloc] initWithRootViewController:[[StudentListTableViewController alloc] init]];
    StudentListTableViewController * stu_list = [[StudentListTableViewController alloc] init];
    stu_list.operate = 1;
    [self.navigationController pushViewController:stu_list animated:YES];
}

- (void) updateStulist:(NSNotification*)notification
{
    self.stu_list = [self.stu_list_obj getStudentList];
    NSArray * subviews = [self.view subviews];
    for (UIView * tmpView in subviews) {
        [tmpView removeFromSuperview];
    }
    DLog(@"%@",self.stu_list);
    if([self.stu_list count] > 0){
        
        //        TabController = nil;
        //        UIView * view = [self.view viewWithTag:1];
        //        [view removeFromSuperview];
        
        [self NavSetting];
        self.nav_view = [[UINavigationController alloc] initWithRootViewController:[self viewController:0]];
        self.nav_view.navigationBarHidden = YES;
        self.nav_view.delegate = self;
        [self addChildViewController:self.nav_view];
        [self.nav_view didMoveToParentViewController:self];
        [self.view addSubview:self.nav_view.view];
        UIScreen * currentScreen = [UIScreen mainScreen];
        self.nav_view.view.frame = CGRectMake(0, 100, 320, currentScreen.applicationFrame.size.height - 40);
    }else{
        
        [Tool showMessage:@"您还未添加学生!"];
        RootViewController * RootView = (RootViewController *)self.navigationController.parentViewController;
        RootView.selectedIndex = 2;
        //        UITextView * text = [[UITextView alloc] initWithFrame:CGRectMake(0, 200, 320, 500)];
        //        text.text = @"您还未添加看管的学生，请前去设置选项卡添加!";
        //        [self.view addSubview:text];
    }
}

- (void)viewDidAppear:(BOOL)animated
{
   if([self.stu_list count] > 0){
   }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) NavSetting
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    TabController = [[DKScrollingTabController alloc] init];
    TabController.delegate = self;
    [self addChildViewController:TabController];
    [TabController didMoveToParentViewController:self];
    TabController.view.tag = 1;
    [self.view addSubview:TabController.view];
    TabController.view.frame = CGRectMake(0, 60, 320, 40);
    
    TabController.view.backgroundColor = [UIColor whiteColor];
    TabController.buttonPadding = 10;
    TabController.underlineIndicator = YES;
    TabController.underlineIndicatorColor = [UIColor colorWithRed:0.110 green:0.573 blue:0.980 alpha:1.0];
    TabController.buttonsScrollView.showsHorizontalScrollIndicator = NO;
    TabController.selectedBackgroundColor = [UIColor whiteColor];
    TabController.selectedTextColor = [UIColor blackColor];
    TabController.unselectedTextColor = [UIColor grayColor];
    TabController.unselectedBackgroundColor = [UIColor whiteColor];
    
    
    NSUInteger num = self.stu_list.count;
    NSMutableArray * data = [NSMutableArray arrayWithCapacity:num];
    for (int a = 0; a < num; a++) {
        data[a] = @"PLACE\n0";
    }
    TabController.selection = data;
    for (int a = 0; a < num; a++) {
        Student * stu = self.stu_list[a];
        NSString * name = stu.name;
        NSString * relation = stu.relation;
        NSString * str = [relation stringByAppendingFormat:@"\n%@",name];
        [TabController setButtonName:str atIndex:a];
    }
    
    
    [TabController.buttons enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        UIButton *button = obj;
        button.titleLabel.numberOfLines = 2;
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        NSString *buttonName = button.titleLabel.text;
        NSString *text =  [buttonName substringWithRange: NSMakeRange(0, [buttonName rangeOfString: @"\n"].location)];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:buttonName];
        NSDictionary *attributes = @{ NSFontAttributeName : [UIFont systemFontOfSize:8] };
        NSRange range = [buttonName rangeOfString:text];
        [attributedString addAttributes:attributes range:range];
        
        button.titleLabel.text = @"";
        [button setAttributedTitle:attributedString forState:UIControlStateNormal];
    }];
}

#pragma mark - TabControllerDelegate

- (void)DKScrollingTabController:(DKScrollingTabController *)controller selection:(NSUInteger)selection {
    
    if (waitNavigation) {
        return;
    }
    waitNavigation =YES;
    ContentViewController * controller_view;
//
//    if([self.palette_data objectForKey:[NSNumber numberWithInteger:selection]]) {
//        controller_view = [self.palette_data objectForKey:[NSNumber numberWithInteger:selection]];
//    } else {
//        controller_view = [self viewController:selection];
//    }
//    NSArray * data = self.nav_view.viewControllers;
//    if([data containsObject:controller_view] == YES){
//        [self.nav_view popToViewController:controller_view animated:YES];
//    }else{
//        [self.nav_view pushViewController:controller_view animated:YES];
//    }
    
    self.nav_view.viewControllers = @[];
//    [self.nav_view popToRootViewControllerAnimated:NO];
    
    controller_view = [self viewController:selection];
    [self.nav_view pushViewController:controller_view animated:YES];

}

//详细信息视图显示
- (ContentViewController *) viewController:(NSUInteger)selection
{
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[dat timeIntervalSince1970];
    NSInteger time = (NSInteger) a;
    ContentViewController * view_box = [[ContentViewController alloc] initWhithStu:self.stu_list[selection] andTime:time];
    view_box.tabs = self;
    view_box.detegate = (NoticeNavViewController *)self.navigationController;
    return view_box;
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    waitNavigation = NO;
}

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
    
    if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft) {
        
        NSInteger index = 0;
        if(TabController.selectIndex > 0){
            index = TabController.selectIndex - 1;
            [TabController selectButtonWithIndex:index delegate:YES];
        }
        
    }
    
    if(recognizer.direction==UISwipeGestureRecognizerDirectionRight) {
        
        NSInteger index = [TabController.buttons count];
        if(TabController.selectIndex < index - 1){
            index = TabController.selectIndex + 1;
            [TabController selectButtonWithIndex:index delegate:YES];
        }
        //执行程序
        
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
